﻿using System;
using System.Collections.Generic;
using System.Web;
using LosConn.BLL;
using System.Web.SessionState;
using Newtonsoft.Json;
using System.Data;

namespace LosConn.Web.Handler
{
    /// <summary>
    /// DataHandler 的摘要说明
    /// </summary>
    public class DataHandler : IHttpHandler, IReadOnlySessionState
    {
        private BLL.Los_Guest bll = new BLL.Los_Guest();
        private BLL.Los_Question bllQue = new BLL.Los_Question();
        private BLL.Los_Data bllData = new Los_Data();
        public void ProcessRequest(HttpContext context)
        {
            String strResult = String.Empty;
            switch (context.Request["fun"])
            {

                case "f_0001": //新增客人信息
                    strResult = AddData(context);
                    break;
                case "f_0002": //审核
                    strResult = UpdateGuestCheck(context);
                    break;
                case "f_0003": //确认
                    strResult = UpdateGuestConfirm(context);
                    break;
                //case "f_0004": //新增产品类别
                //    strResult = AddType(context);
                //    break;
                //case "f_0005": //修改产品类别
                //    strResult = UpdateType(context);
                //    break;
                //case "f_0006": //假删除产品类别
                //    strResult = UpdateTypeStatus(context);
                //    break;
                default:
                    break;
            }

            context.Response.Write(strResult);
            context.Response.End();
        }

        #region 私有方法
        /// <summary>
        /// 新增产品
        /// </summary>
        private string AddData(HttpContext context)
        {
            Model.Los_Guest model = JsonConvert.DeserializeObject<Model.Los_Guest>(context.Request["data"]);
            String Guest_Case = context.Request.Form["dataHTML"];
            Dictionary<String, String> dic = JsonConvert.DeserializeObject<Dictionary<String, String>>(context.Request["data"]);
            model.Guest_UserID = Convert.ToInt32(context.Session["UserID"]);
            model.Guest_UserName = context.Session["UserName"].ToString();
            model.Guest_RetailID = Convert.ToInt32(context.Session["Retail_ID"]);
            model.Guest_Case = Guest_Case;
            Int32 Guest_ID = bll.AddGuest(model);
            if (Guest_ID > 0)
            {
                //model.Check_GuestID = Guest_ID;
                //model.EyeCheck_GuestID = Guest_ID;
                //model.RetinaCheck_GuestID = Guest_ID;
                //bll.AddCheck(model);
                //bll.AddEyeCheck(model);
                //bll.AddRetinaCheck(model);


                Model.Los_QueAns modelQue = null;// new Model.Los_QueAns();
                //DataTable dt = bllData.GetAnswerList("").Tables[0];
                DataTable dtQuestion = bllData.GetQuestionList("").Tables[0];
                foreach (DataRow dr in dtQuestion.Rows)
                {
                    modelQue = new Model.Los_QueAns();
                    if (!dr["Question_Choose"].ToString().Equals("2") && !dr["Question_Choose"].ToString().Equals("3"))
                    {
                        modelQue.QueAns_AnswerID = dic["AI" + dr["Question_ID"].ToString()];//context.Request.Form["AI" + dr["Question_ID"].ToString()];// context.Request[dr[""]]
                    }
                    else
                    {
                        modelQue.QueAns_AnswerID = "";
                    }
                    modelQue.QueAns_AnswerName = dic["AN" + dr["Question_ID"].ToString()];//context.Request.Form["AN" + dr["Question_ID"].ToString()];
                    modelQue.QueAns_GuestID = Guest_ID;
                    modelQue.QueAns_QuestionID = Convert.ToInt32(dic[dr["Question_ID"].ToString()]);//Convert.ToInt32(context.Request.Form[dr["Question_ID"].ToString()]);
                    modelQue.QueAns_QuestionName = dic["N" + dr["Question_ID"].ToString()];//context.Request.Form["N" + dr["Question_ID"].ToString()];
                    modelQue.QueAns_UserID = Convert.ToInt32(context.Session["UserID"]);
                    modelQue.QueAns_UserName = context.Session["UserName"].ToString();
                    bllQue.AddQueAns(modelQue);
                }

                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";
            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }

        /// <summary>
        /// 审核
        /// </summary>
        private string UpdateGuestCheck(HttpContext context)
        {
            Model.Los_Guest model = new Model.Los_Guest();// JsonConvert.DeserializeObject<Model.Los_Guest>(context.Request["data"]);
            //String 
            model.Guest_AuditRemark = context.Request.Form["Guest_AuditRemark"];
            model.Guest_Stauts = Convert.ToInt32(context.Request.Form["Guest_Stauts"]);
            model.Guest_AuditUserID = Convert.ToInt32(context.Session["UserID"]);
            model.Guest_AuditUserName = context.Session["UserName"].ToString();
            model.Guest_ID = Convert.ToInt32(context.Request.Form["id"]);
            if (bll.UpdateGuestCheck(model))
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }

        /// <summary>
        /// 确认
        /// </summary>
        private string UpdateGuestConfirm(HttpContext context)
        {
            Model.Los_Guest model = new Model.Los_Guest();// JsonConvert.DeserializeObject<Model.Los_Guest>(context.Request["data"]);
            //String 
            model.Guest_ConfirmRemark = context.Request.Form["Guest_ConfirmRemark"];
            model.Guest_Stauts = Convert.ToInt32(context.Request.Form["Guest_Stauts"]);
            model.Guest_ConfirmUserID = Convert.ToInt32(context.Session["UserID"]);
            model.Guest_ConfirmUserName = context.Session["UserName"].ToString();
            model.Guest_ID = Convert.ToInt32(context.Request.Form["id"]);
            if (bll.UpdateGuestConfirm(model))
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}